<template>
  <div class="staff-dashboard">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <div class="welcome-content">
        <div class="welcome-text">
          <h1>欢迎回来，{{ dashboardData.userInfo?.name || userStore.userName }}</h1>
          <p>今天是 {{ currentDate }}，祝您工作愉快！</p>
          <div class="user-info">
            <el-tag v-if="dashboardData.userInfo?.department" type="info" size="small">
              {{ dashboardData.userInfo.department }}
            </el-tag>
          </div>
        </div>
        <div class="welcome-actions">
          <el-button type="primary" @click="refreshData" :loading="loading">
            <el-icon><Refresh /></el-icon>
            刷新数据
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-row" v-loading="loading">
      <el-col :span="6">
        <el-card class="stat-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon teaching">
              <el-icon><Reading /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ dashboardData.personalStats?.myTeachingProjects || 0 }}</h3>
              <p>教学项目</p>
              <span class="stat-trend">{{ dashboardData.yearlyStats?.yearlyTeachingProjects || 0 }} 本年度</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card class="stat-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon research">
              <el-icon><Document /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ dashboardData.personalStats?.myResearchProjects || 0 }}</h3>
              <p>科研项目</p>
              <span class="stat-trend">{{ dashboardData.yearlyStats?.yearlyResearchProjects || 0 }} 本年度</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card class="stat-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon awards">
              <el-icon><Trophy /></el-icon>
            </div>
            <div class="stat-info">
              <h3>{{ dashboardData.personalStats?.myAwards || 0 }}</h3>
              <p>获得奖项</p>
              <span class="stat-trend">累计获得</span>
            </div>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card class="stat-card" shadow="hover">
            <div class="stat-content">
              <div class="stat-icon achievements">
                <el-icon><Trophy /></el-icon>
              </div>
              <div class="stat-info">
                <h3>{{ dashboardData.personalStats?.academicAchievements || 0 }}</h3>
                <p>学术成果</p>
                <span class="stat-trend">论文专利</span>
              </div>
            </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 主要内容区域 -->
    <el-row :gutter="20" class="content-row">
      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span><el-icon><Document /></el-icon> 最近提交的项目</span>
              <el-button type="text" @click="$router.push('/staff/teaching-projects')">
                查看更多 <el-icon><ArrowRight /></el-icon>
              </el-button>
            </div>
          </template>

          <div v-if="dashboardData.recentProjects?.length > 0">
            <el-table :data="dashboardData.recentProjects" stripe size="small">
              <el-table-column prop="name" label="项目名称" show-overflow-tooltip />
              <el-table-column prop="type" label="类型" width="80">
                <template #default="{ row }">
                  <el-tag :type="row.type === '教学' ? 'primary' : 'success'" size="small">
                    {{ row.type }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="date" label="提交时间" width="100" />
              <el-table-column prop="status" label="状态" width="80">
                <template #default="{ row }">
                  <el-tag :type="getStatusType(row.status)" size="small">
                    {{ row.status }}
                  </el-tag>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div v-else class="empty-state">
            <el-empty description="暂无项目数据" :image-size="80" />
          </div>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span><el-icon><Clock /></el-icon> 系统公告</span>
              <el-button type="text" size="small" @click="refreshData">
                刷新 <el-icon><Refresh /></el-icon>
              </el-button>
            </div>
          </template>

          <div class="announcement-list">
            <div v-if="dashboardData.announcements?.length > 0">
              <div v-for="announcement in dashboardData.announcements.slice(0, 4)" :key="announcement.id" class="announcement-item">
                <div class="announcement-content">
                  <div class="announcement-header">
                    <el-tag :type="getAnnouncementTypeColor(announcement.type)" size="small">{{ announcement.type }}</el-tag>
                    <span class="announcement-time">{{ formatTime(announcement.time) }}</span>
                  </div>
                  <h4 class="announcement-title">{{ announcement.title }}</h4>
                  <p class="announcement-desc">{{ announcement.description }}</p>
                </div>
              </div>
            </div>
            <div v-else class="empty-state">
              <div class="announcement-item">
                <div class="announcement-content">
                  <div class="announcement-header">
                    <el-tag type="info" size="small">系统通知</el-tag>
                    <span class="announcement-time">{{ formatTime(new Date()) }}</span>
                  </div>
                  <h4 class="announcement-title">欢迎使用教师信息管理系统</h4>
                  <p class="announcement-desc">请及时更新您的个人信息和项目数据</p>
                </div>
              </div>
              <div class="announcement-item">
                <div class="announcement-content">
                  <div class="announcement-header">
                    <el-tag type="warning" size="small">提醒</el-tag>
                    <span class="announcement-time">{{ formatTime(new Date()) }}</span>
                  </div>
                  <h4 class="announcement-title">数据备份提醒</h4>
                  <p class="announcement-desc">建议定期导出重要数据进行备份</p>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <template #header>
            <span>快速操作</span>
          </template>

          <div class="quick-actions">
            <el-button
                type="primary"
                @click="$router.push('/staff/teaching-projects')"
            >
              <el-icon><Plus /></el-icon>
              新增教学项目
            </el-button>

            <el-button
                type="success"
                @click="$router.push('/staff/research-projects')"
            >
              <el-icon><Plus /></el-icon>
              新增科研项目
            </el-button>

            <el-button
                type="warning"
                @click="$router.push('/staff/awards')"
            >
              <el-icon><Plus /></el-icon>
              新增奖项信息
            </el-button>

            <el-button
                type="info"
                @click="$router.push('/staff/activities')"
            >
              <el-icon><Plus /></el-icon>
              新增学术活动
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import {
  Reading,
  Document,
  Trophy,
  Clock,
  Plus,
  Refresh,
  ArrowRight,
  Bell
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const userStore = useUserStore()

// 响应式数据
const loading = ref(false)
const dashboardData = ref({})

const currentDate = computed(() => {
  const now = new Date()
  return now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })
})

// 获取仪表板数据
const fetchDashboardData = async () => {
  loading.value = true
  try {
    const response = await request.get('/api/dashboard/staff/overview')
    dashboardData.value = response
  } catch (error) {
    console.error('获取仪表板数据失败:', error)
    ElMessage.error('获取数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 刷新数据
const refreshData = async () => {
  await fetchDashboardData()
  ElMessage.success('数据已刷新')
}

// 获取公告类型颜色
const getAnnouncementTypeColor = (type) => {
  const colors = {
    '系统通知': 'info',
    '重要公告': 'danger',
    '提醒': 'warning',
    '更新': 'success'
  }
  return colors[type] || 'info'
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  const now = new Date()
  const diff = now - date
  
  if (diff < 60000) return '刚刚'
  if (diff < 3600000) return `${Math.floor(diff / 60000)}分钟前`
  if (diff < 86400000) return `${Math.floor(diff / 3600000)}小时前`
  if (diff < 2592000000) return `${Math.floor(diff / 86400000)}天前`
  
  return date.toLocaleDateString('zh-CN')
}

const getStatusType = (status) => {
  const types = {
    '已通过': 'success',
    '审核中': 'warning', 
    '待审核': 'info',
    '已拒绝': 'danger',
    '进行中': 'primary',
    '已完成': 'success'
  }
  return types[status] || 'info'
}

// 组件挂载时获取数据
onMounted(() => {
  fetchDashboardData()
})
</script>

<style scoped>
.staff-dashboard {
  max-width: 1200px;
}

/* 欢迎横幅样式 */
.welcome-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 32px;
  border-radius: 16px;
  margin-bottom: 32px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
}

.welcome-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.welcome-text h1 {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 600;
}

.welcome-text p {
  margin: 0 0 16px 0;
  font-size: 16px;
  opacity: 0.9;
}

.user-info {
  display: flex;
  gap: 8px;
}

.welcome-actions {
  display: flex;
  gap: 12px;
}

.stats-row {
  margin-bottom: 24px;
}

.stat-card {
  border-radius: 12px;
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;
  overflow: hidden;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stat-icon.teaching {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.research {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.awards {
  background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}

.stat-icon.achievements {
  background: linear-gradient(135deg, #ffeaa7 0%, #fab1a0 100%);
}

.stat-info h3 {
  margin: 0 0 4px 0;
  font-size: 28px;
  font-weight: 700;
  color: #333;
  line-height: 1;
}

.stat-info p {
  margin: 0 0 4px 0;
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.stat-trend {
  font-size: 12px;
  color: #999;
  font-weight: 400;
}

.content-row {
  margin-bottom: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: #333;
}

.card-header .el-icon {
  margin-right: 8px;
  color: #667eea;
}

.announcement-list {
  max-height: 300px;
  overflow-y: auto;
}

.announcement-item {
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
  transition: background-color 0.2s;
}

.announcement-item:hover {
  background-color: #fafafa;
  margin: 0 -16px;
  padding-left: 16px;
  padding-right: 16px;
  border-radius: 8px;
}

.announcement-item:last-child {
  border-bottom: none;
}

.announcement-content {
  width: 100%;
}

.announcement-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.announcement-time {
  color: #999;
  font-size: 12px;
}

.announcement-title {
  margin: 0 0 4px 0;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  line-height: 1.4;
}

.announcement-desc {
  margin: 0;
  font-size: 12px;
  color: #666;
  line-height: 1.4;
}

.quick-actions {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.quick-actions .el-button {
  flex: 1;
  min-width: 140px;
  height: 48px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
}

/* 空状态样式 */
.empty-state {
  padding: 20px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-content {
    flex-direction: column;
    text-align: center;
    gap: 16px;
  }
  
  .stats-row .el-col {
    margin-bottom: 16px;
  }
  
  .content-row .el-col {
    margin-bottom: 20px;
  }
}

/* 加载动画 */
.el-loading-mask {
  border-radius: 12px;
}

/* 卡片阴影优化 */
.el-card {
  transition: all 0.3s ease;
}

.el-card:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

/* 按钮样式优化 */
.welcome-actions .el-button {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  backdrop-filter: blur(10px);
}

.welcome-actions .el-button:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}
</style>